// pages/Index/shopActive/friendCards/friendCards.js
const controllers = require('../../../controllers/controllerIndex').controller

const controller=require("../../../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList:[
      {
        name:"优惠券列表",
        id:1
      },{
        name:"引流卡列表",
        id:2
      }
    ],
    selectIndex:0,
    pageNumber:1,
    couponList:[],
    cardList:[],
    lastPage:false,
    shopImg:""
  },

  //跳转引流卡编辑页
  setCard(e){
    let index=e.currentTarget.dataset.index
    wx.navigateTo({
      url: '/pages/Index/shopActive/friendCards/addFriendCards?id='+this.data.cardList[index].id,
    })
  },



  //删除引流卡
  delCard(e){
    let index=e.currentTarget.dataset.index
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    value.vipId=this.data.cardList[index].id
    let that=this

    wx.showModal({
      title: '提示',
      content: '是否确认删除？',
      success (res) {
        if (res.confirm) {
          controller.vipdelete(value).then(res=>{
            wx.showToast({
              title: '删除成功',
            })
            that.getCardList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //删除过期优惠券
  delCoupon(e){
    let index=e.currentTarget.dataset.index
    let value={}
    value.couponId=this.data.couponList[index].id
    value.shopId=wx.getStorageSync('shopId')
    let that=this


    wx.showModal({
      title: '提示',
      content: '是否确认删除？',
      success (res) {
        if (res.confirm) {
          controller.delCoupon(value).then(res=>{
            wx.showToast({
              title: '删除成功',
            })
            that.getCouponList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //跳转赠送详情
  toGiveHistory(e){
      let id=e.currentTarget.dataset.id
      wx.navigateTo({
        url: '/pages/Index/shopActive/friendCards/giveHistory?id='+id,
      })
  },

  changeTab(e){
    let index=e.currentTarget.dataset.index
    this.setData({
      selectIndex:index
    })
    if(index==0){
      this.getCouponList()
    }else{
      this.getCardList()
    }
  },

  //赠送优惠券
  toGivePerson(e){
    let id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/Index/shopActive/friendCards/giveFriendCards?id='+id,
    })
  },

  //跳转新增页面
  toAddPage(){
    if(this.data.selectIndex==0){
      wx.navigateTo({
        url: '/pages/Index/couponActivity/addCoupon/index?isVip=1',
      })
    }else{
      wx.navigateTo({
        url: '/pages/Index/shopActive/friendCards/addFriendCards',
      })
    }
  },

  //获取优惠券列表
  getCouponList(){

    const newTime = new Date().getTime()
        
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    value.pageNumber=this.data.pageNumber
    value.isVip=1
    controllers.getCouponList(value).then(res=>{
      res.data.list.forEach(item=>{
        item.isOverdue = false
        if(item.timeState==1){
          const endTimeStamp=Date.parse(item.effectiveEndTime);
          if(endTimeStamp < newTime) item.isOverdue = true

          item.effectiveEndTime=item.effectiveEndTime.split(" ")[0]
          item.effectiveStartTime=item.effectiveStartTime.split(" ")[0]
        }
      }) 

      this.setData({
        couponList:res.data.list,
        lastPage:res.data.lastPage
      })
    })
  },

  //获取引流卡列表
  getCardList(){
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    value.types="4"
    controller.viplist(value).then(res=>{
      this.setData({
        cardList:res.data
      })
    })
  },

  //跳转会员卡详情
  toCardDetail(e){
    let id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/diyActivity/detail/detail?id='+id,
    })
  },

  toDetail(e) {
    wx.navigateTo({
      url: '/pages/Index/couponActivity/couponDetail/index?id=' + e.currentTarget.id + '&ispush=' + e.currentTarget.dataset.ispush,
    })
  },

  getMore(){
    if(this.data.selectIndex==0){
      if(this.data.lastPage==false){
        let value={}
        value.shopId=wx.getStorageSync('shopId')
        value.pageNumber=this.data.pageNumber+1
        this.setData({
          pageNumber:value.pageNumber
        })
        value.isVip=1
        controllers.getCouponList(value).then(res=>{
          res.data.list.forEach(item=>{
            if(item.timeState==1){
              item.effectiveEndTime=item.effectiveEndTime.split(" ")[0]
              item.effectiveStartTime=item.effectiveStartTime.split(" ")[0]
            }
          }) 
          let list=this.data.couponlist
          let list2=res.data.list
          let list3=list.concat(list2)
          this.setData({
            couponList:list3,
            lastPage:res.data.lastPage
          })
        })
      }
    }else{

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopImg:wx.getStorageSync('shopImg')
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCouponList()
    this.getCardList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMore()
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})